package com.bdqn.BookManage.servlet;

import com.bdqn.BookManage.view.BookInfoView;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @author : CP
 * @version: 1.0
 * @program : BookManage
 * @description : 删除购物车中的信息处理器
 * @date : 2020-07-30 09:22
 **/
@WebServlet("/deleteSession")
public class DeleteSessionServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        /** 提取购物车信息 */
        HttpSession session = req.getSession();
        Map<String, BookInfoView> cart = (Map<String, BookInfoView>) session.getAttribute("cart");

        /** 根据商品编号进行删除 */
        String id = req.getParameter("id");
        cart.remove(id);

        session.setAttribute("cart",cart);
        resp.sendRedirect("shoppingTrolley.jsp");
    }
}
